package com.xiaoqianxin.pang_rowth.pang_rowth_plugin

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.bytedance.sdk.djx.DJXSdk
import com.bytedance.sdk.djx.interfaces.listener.IDJXAdListener
import com.bytedance.sdk.djx.interfaces.listener.IDJXDramaHomeListener
import com.bytedance.sdk.djx.interfaces.listener.IDJXDramaListener
import com.bytedance.sdk.djx.interfaces.listener.IDJXDramaUnlockListener
import com.bytedance.sdk.djx.model.DJXDrama
import com.bytedance.sdk.djx.model.DJXDramaDetailConfig
import com.bytedance.sdk.djx.model.DJXDramaUnlockAdMode
import com.bytedance.sdk.djx.model.DJXDramaUnlockInfo
import com.bytedance.sdk.djx.model.DJXDramaUnlockMethod
import com.bytedance.sdk.djx.params.DJXWidgetDramaHomeParams
import com.xiaoqianxin.pang_rowth.pang_rowth_plugin.utils.DefaultAdListener
import io.flutter.plugin.common.MethodChannel.Result

class DramaActivity : Activity() {

    companion object {
        private const val TAG = "DramaActivity"

        fun start(context: Context) {
            val intent = Intent(context, DramaActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 设置全屏显示
        window.statusBarColor = android.graphics.Color.BLACK

        loadDramaContent()
    }

    private fun loadDramaContent() {
        val detailConfig = DJXDramaDetailConfig.obtain(
            DJXDramaUnlockAdMode.MODE_COMMON,
            5, // FREE_SET
            object : IDJXDramaUnlockListener {
                override fun unlockFlowEnd(
                    drama: DJXDrama,
                    errCode: IDJXDramaUnlockListener.UnlockErrorStatus?,
                    map: Map<String, Any>?
                ) {
                    Log.d(TAG, "unlockFlowEnd")
                }

                override fun unlockFlowStart(
                    drama: DJXDrama,
                    callback: IDJXDramaUnlockListener.UnlockCallback,
                    map: Map<String, Any>?
                ) {
                    Log.d(TAG, "unlockFlowStart")
                    val unlockInfo =
                        DJXDramaUnlockInfo(drama.id, 2, DJXDramaUnlockMethod.METHOD_AD, false)
                    callback.onConfirm(unlockInfo)
                }
            })

        detailConfig.adListener(DefaultAdListener(object : IDJXAdListener() {

            override fun onDJXAdRequestSuccess(p0: MutableMap<String, Any>?) {
                super.onDJXAdRequestSuccess(p0)
                Log.d(TAG, "onDJXAdRequestSuccess: success: $p0")
            }

            override fun onDJXAdRequestFail(p0: Int, p1: String?, p2: MutableMap<String, Any>?) {
                super.onDJXAdRequestFail(p0, p1, p2)
                Log.d(TAG, "onDJXAdRequestSuccess: fail:$p0 $p1 $p2")
            }
        }))

        detailConfig.listener(object : IDJXDramaListener() {
            override fun onDJXClose() {
                super.onDJXClose()
                Log.d(TAG, "onDJXClose: 用户关闭短剧，结束Activity")
            }

            override fun onDJXRequestSuccess(list: MutableList<MutableMap<String, Any>>?) {
                Log.d(TAG, "onDJXRequestSuccess: 短剧加载成功")
                super.onDJXRequestSuccess(list)
            }

            override fun onDJXRequestFail(code: Int, msg: String?, map: MutableMap<String, Any>?) {
                super.onDJXRequestFail(code, msg, map)
                Log.e(TAG, "onDJXRequestFail: 短剧加载失败 $code $msg")
                // 加载失败，关闭Activity
            }
        })

        val dpWidget = DJXSdk.factory().createDramaHome(
            DJXWidgetDramaHomeParams.obtain(detailConfig)
                .listener(object : IDJXDramaHomeListener() {
                    override fun onItemClick(drama: DJXDrama?, map: MutableMap<String, Any>?) {
                        super.onItemClick(drama, map)
                        drama ?: return
                        map ?: return
                        Log.d(TAG, "用户点击短剧: ${drama.title}")
                    }
                })
                .showBackBtn(true)
        )

        val fragment = dpWidget.fragment2

        // 将Fragment添加到当前Activity
        fragmentManager?.beginTransaction()
            ?.add(android.R.id.content, fragment, "DJX_FRAGMENT")
            ?.commitAllowingStateLoss()
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}